<template>
  <div class="search-box">
    <div class="top">
      <div class="top-title">搜索结果</div>
    </div>
    <div class="content">
      <div class="search-loading" v-if="searchLoading">
        <img
          src="https://image.xuexiwangzhe.com/FpY-VrUXyjV5LqtvGu0an1rp0HUX-720"
        />
      </div>
      <div
        class="none"
        v-else-if="!searchData.length && !searchLoading && searchempty"
      >
        <img
          src="https://image.xuexiwangzhe.com/FmmDvVh1x7DXMkviRr0f3HBTKA12"
        />
        <div class="text">
          暂未搜索到相关课程
        </div>
      </div>
      <div v-else>
        <list
          v-bind="$attrs"
          v-on="$listeners"
          :data="searchData"
          :createProjectShow="true"
        ></list>
      </div>
    </div>
  </div>
</template>

<script>
import list from "./list.vue";
import { mapState } from "vuex";
export default {
  components: {
    list,
  },
  data() {
    return {
      searchempty: false,
      searchLoading: false,
      searchData: {},
      page: 1,
      size: 100,
      searchContent: "",
    };
  },
  computed: mapState(["listReachBottom"]),
  watch: {
    listReachBottom(newVal, oldVal) {
      if (!newVal) return;
      this.searchProject();
    },
  },
  methods: {
    search(content) {
      this.page = 1;
      this.searchData = {
        folders: [],
        projects: [],
      };
      this.searchContent = content;
      this.searchLoading = true;
      this.searchProject();
    },
    searchProject() {
      const params = {
        search: this.searchContent,
        page: this.page,
        size: this.size,
      };
      this.$axios.get("index/search", { params }).then((res) => {
        if (res.success) {
          if (res.data.projectInfoVoPageList || res.data.folderVoList) {
            this.searchempty = false;
            if (
              !this.searchData.folders ||
              this.searchData.folders.length <= 0
            ) {
              this.searchData.folders = res.data.folderVoList;
            }
            if (res.data.projectInfoVoPageList) {
              this.searchData.projects = [
                ...this.searchData.projects,
                ...res.data.projectInfoVoPageList.content,
              ];
              this.page += 1;
            }
          } else {
            this.searchempty = true;
          }
        }
        this.searchLoading = false;
      });
    },
  },
};
</script>

<style lang="less" scoped>
.search-box {
  width: 100%;
  min-height: calc(100vh - 85px);
  display: flex;
  flex-direction: column;
  position: absolute;
  top: 0;
  z-index: 999;
  background: #f2f4f8;

  .top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 38px;
  }
  .top-title {
    font-size: 15px;
    color: #333;
    margin-right: 14px;
    font-weight: bold;
  }
  .none {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-weight: bold;
    color: #333333;
    text-align: center;
    img {
      width: 210px;
      height: 210px;
    }
    .text {
      margin-top: 12px;
    }
  }
  .search-loading img {
    width: 202px;
    height: 159px;
    position: absolute;
    top: 223px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1;
  }
}
</style>
